<?php

class GroupController extends Zend_Controller_Action
{

    public function init()
    {
        parent::init();

        if (Zend_Auth::getInstance()->hasIdentity())
        {
            $user = Zend_Auth::getInstance()->getIdentity();

            $this->view->userProfile = $user->profile;
        }
        else
            $this->_redirect('/index');
    }

    public function indexAction()
    {
        $this->view->friends = Friendship::findAllFriends();
        $this->view->groups = UserGroup::findAll();
    }

    public function editgroupAction()
    {
        $group = UserGroup::findOneById($this->_getParam('id'));

        //chekeo si el idGrupo corresponde a un grupo del usuario logeado
        $user = Zend_Auth::getInstance()->getIdentity();
        if ($group->id_user != $user->id)
        {
            //si entra aca el grupo no me pertenece XD
            $this->_redirect('/home/index');
        }
        $this->view->friends = Friendship::findAllFriends();
        $this->view->group = $group;
    }

    public function deletegroupAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $idGroup = $this->_getParam('id');

        
        //chekeo si existe el grupo
        self::checkGroupExistence($idGroup);
        //chekeo si el grupo corresponde a un grupo del usuario logeado
        self::checkGroupIsMine($idGroup);

        $group = UserGroup::findOneById($idGroup);
        
        $user = Zend_Auth::getInstance()->getIdentity();

        if ($group->user->id != $user->id)
        {
            die("no me pertenece!!!!");
            //si entra aca el grupo no me pertenece XD
            $this->_redirect('/home/index');
        }

        $friendships = Friendship::findAllByGroup($idGroup);

        foreach ($friendships as $clearFriendship)
        {
            $clearFriendship->id_user_group = null;
            $clearFriendship->save();
        }

        $groupInstance = UserGroup::findOneById($idGroup);
        $groupInstance->delete();

        $this->_redirect('/group/index');
    }

    public function saveAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $name = $this->_getParam('name');
        $friends = $this->_getParam('friends');

        //chekeo que los amigos sean realmente amigos

        try
        {
            $conn = Doctrine_Manager::connection();
            $conn->beginTransaction();

            $group = new UserGroup();
            $group->name = $name;
            $group->user = Zend_Auth::getInstance()->getIdentity();
            $group->save();

            foreach ($friends as $friendId)
            {
                $friendship = Friendship::findOneByFriend($friendId);
                $friendship->group = $group;
                $friendship->save();
            }

            $wall = Wall::findOneByLoggedUser();

            $permissions = new GroupPermission();
            $permissions->group = $group;
            $permissions->id_object = $wall->object->id;
            $permissions->permission = GroupPermission::READ_WRITE;
            $permissions->save();

            $conn->commit();

            $this->_redirect('group/index');
        } catch (Exception $e)
        {
            $conn->rollback();

            echo $e->getMessage();
        }
    }

    public function updateAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $name = $this->_getParam('name');
        $friends = $this->_getParam('friends');
        $idGroup = $this->_getParam('id');

        //chekeo si el idGrupo corresponde a un grupo del usuario logeado
        $user = Zend_Auth::getInstance()->getIdentity();
        $group = UserGroup::findOneById($idGroup);
        if ($group->id_user != $user->id)
        {
            //si entra aca el grupo no me pertenece XD
            $this->_redirect('/home/index');
        }

        //chekeo si los friends son realmente friends
        foreach ($friends as $id_friends)
        {
            $friendshipStatus = Friendship::checkFriendStatus($id_friends);
            if ($friendshipStatus != Friendship::ARE_FRIEND)
            {
                $this->_redirect('/home/index');
            }
        }


        try
        {
            $conn = Doctrine_Manager::connection();
            $conn->beginTransaction();

            $group = UserGroup::findOneById($idGroup);
            $group->name = $name;
            $group->save();

            $friendships = Friendship::findAllByGroup($idGroup);

            foreach ($friendships as $clearFriendship)
            {
                $clearFriendship->id_user_group = null;
                $clearFriendship->save();
            }

            foreach ($friends as $friendId)
            {
                $friendship = Friendship::findOneByFriend($friendId);
                $friendship->group = $group;
                $friendship->save();
            }

            $conn->commit();

            $this->_redirect('group/index');
        } catch (Exception $e)
        {
            $conn->rollback();

            echo $e->getMessage();
        }
    }

    private function checkGroupIsMine($idGroup)
    {
        $group = UserGroup::findOneById($idGroup);
        
        $user = Zend_Auth::getInstance()->getIdentity();

        if ($group->user->id != $user->id)
        {
            $this->_redirect("/home/index");
        }
    }

    private function checkGroupExistence($idGroup)
    {
        $group = UserGroup::findOneById($idGroup);
        
        if ($group == null)
        {
            $this->_redirect('/home/index');
        }
    }

}